<template>
  <el-dialog
    :title="title"
    :visible.sync="isShow"
    width="60%"
    @close="reset"
  >
    <!-- 匿名插槽 -->
    <el-form ref="deptForm" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="1-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="(val,index) in peoples" :key="index" :label="val.username" :value="val.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button size="small" @click="isShow = false">取 消</el-button>
      <el-button size="small" type="primary" @click="btnOk">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  name: 'AddDept',
  props: {
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkNameRepeat = async(rules, value, cb) => {
      const { depts } = await getDepartments()
      let arr = []
      let isRepeat = false
      // 找到子级部门
      if (this.formData.id) {
        // 找到除了我之外 同级的 不能和我同名的
        arr = depts.filter(item => item.pid === this.treeNode.pid)
        isRepeat = arr.some(item => item.name === value && item.id !== this.formData.id)
      } else {
        // 新增
        arr = depts.filter(item => item.pid === this.treeNode.id)
        isRepeat = arr.some(item => item.name === value)
      }

      isRepeat ? cb(new Error(`同级部门下已经有${value}的部门了`)) : cb()
    }
    const checkCodeRepeat = async(rules, value, cb) => {
      const { depts } = await getDepartments()
      console.log(depts)
      let isRepeat = false
      if (this.formData.id) {
        // 除了自己  编辑
        isRepeat = depts.some(item => item.code === value && value && item.id !== this.formData.id)
      } else {
        // 新增模式
        isRepeat = depts.some(item => item.code === value && value)
      }
      // console.log(depts)
      // 找到子级部门
      isRepeat ? cb(new Error(`同级部门下已经有${value}的邮编了`)) : cb()
    }
    const defaultItem = {
      name: '', // 部门名称
      code: '', // 部门编码
      manager: '', // 部门管理者
      introduce: '' // 部门介绍
    }
    return {
      isShow: false,
      formData: { ...defaultItem },
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkNameRepeat }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }

        ],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }
        ]
      },
      peoples: []
    }
  },
  computed: {
    title() {
      return this.formData.id ? '修改部门' : '新增部门'
    }
  },
  methods: {
    async  getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    },
    async btnOk() {
      // 表单验证
      try {
        await this.$refs.deptForm.validate()
      } catch (err) {
        return console.log(err)
      }
      if (this.formData.id) {
        await updateDepartments(this.formData)
      } else {
        this.formData.pid = this.treeNode.id
        // 加工数据
        // 发起请求
        await addDepartments(this.formData)
      }

      this.$message.success('操作成功')
      // this.formData = { ...defaultItem }

      this.isShow = false
      this.$emit('addDepts')
    },
    async getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
    },
    reset() {
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    }
  }

}
</script>

<style>

</style>
